package com.hspedu.streamtest;

import com.hspedu.lambda2.Employee;
import com.hspedu.lambda2.EmployeeData;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/30
 * @time 14:28
 */
public class StreamAPITest {

    /**
     * 方式一：通过集合创建
     */
    @Test
    public void test1() {
        List<Employee> employees = EmployeeData.getEmployees();
        Stream<Employee> stream = employees.stream();
        Stream<Employee> employeeStream = employees.parallelStream();
    }

    /**
     * 方式二：通过数组方式创建
     */
    @Test
    public void test2() {
        int[] arr = {1, 2, 3, 4};
        IntStream stream = Arrays.stream(arr);

        Employee employee = new Employee(1001);
        Employee employee1 = new Employee(1002);
        Employee[] employees = {employee, employee1};
        Stream<Employee> stream1 = Arrays.stream(employees);
    }

    /**
     * 方式三：通过Stream类的of方法
     */
    @Test
    public void test3() {
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
    }

    /**
     * 方式四：创建无限流
     */
    @Test
    public void test4() {
        Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out :: println);
        Stream.generate(Math :: random).limit(10).forEach(System.out :: println);
    }
}
